﻿using CoreComponents;
using Crowler.Model.CoreComponents;
using Crowler.TestBase;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Crowler.CoreComponents.Test
{
    [TestClass]
    public class BusinessServiceDecoratorShould:TestBaseClass
    {
        private const string TestRequest = "TestRequest";
        private BusinessServiceDecorator<TempRequest, TempResponse> _decorator;
        private Mock<IAppEnvironment> _appEnv;

        public override void TestCleanup()
        {
        }

        public override void TestInitialize()
        {
            _appEnv = new Mock<IAppEnvironment>();
            SetUpMocks();
            _decorator = new BusinessServiceDecorator<TempRequest, TempResponse>(_appEnv.Object
                                                                                 );
        }

        private void SetUpMocks()
        {
            _appEnv.Setup(o => o.Resolve<IBusinessService<TempRequest, TempResponse>>()).Returns(
                new DummyService());
        }



        [TestMethod]
        public void DecorateInnerServiceExecution()
        {

            var serializationHelper = new SerializationHelper<TempResponse,TempRequest>();
            string resulting = serializationHelper.GetResponseString(new TempRequest {RequestVal1 = TestRequest});
            _decorator.ServiceRequest = resulting;
            _decorator.Execute();
            var decoratorResult = _decorator.ServiceResponse;
            var finalResult = serializationHelper.GetRequestObject(decoratorResult);
            Assert.AreEqual(TestRequest, finalResult.ResponseVal1);
        }

        public class DummyService : BusinessServiceBase<TempRequest, TempResponse>
        {
            public override void Execute()
            {
                Response = new TempResponse{ResponseVal1 = Request.RequestVal1};
            }

            public override void InitializeDefaultResponse()
            {
                Response = new TempResponse();
            }
        }

        public class TempRequest
        {
            public string RequestVal1 { get; set; }
            public bool RequestVal2{ get; set; } 
        }
        public class TempResponse
        {
            public string ResponseVal1 { get; set; }
            public bool ResponseVal2 { get; set; }
        }
    }
}
